-module(odin_app).
-behaviour(application).

-export([start/2, prep_stop/1, stop/1, init/0]).
-include("odin.hrl")

%%%
%%% Application API
%%%

start(normal, _Args) ->
	init(),
	Sup = odin_sup:start_link(),
	start_modules(),
	Sup;
start(_, _) ->
	{error, badarg}.

%%% This  function  is  called  when  an  application is about to be
%%% stopped, before shutting down the processes of the  application.
prep_stop(State) ->
    stop_modules(),
    State.

%%% This function is called whenever an application has stopped.
stop(_State) ->
    ok.

%%%
%%% Internal functions
%%%

init() ->
    register(odin, self()).

start_modules() ->
	[M:start(Opts) || {M, Opts} <- odin_config:get(modules)].

stop_modules() ->
	[M:stop() || {M, _Opts} <- odin_config:get(modules)].



